package com.example.countscore;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CalculateActivity extends AppCompatActivity {

    private static final String TAG = "CalculateActivity";
    TextView calculateTitle;
    TextView calculateRes;
    EditText calculateRMB;
    String itemTitle;
    String itemDetail;
    Float rate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);

        //使用intent接收点击列表项传输数据
        Intent intent = getIntent();
        itemTitle = intent.getStringExtra("itemTitle");
        itemDetail = intent.getStringExtra("itemDetail");

        Log.i(TAG, "onCreate: itemTitle=" + itemTitle);
        Log.i(TAG, "onCreate: itemDetail=" + itemDetail);

        //显示当前货币名称
        calculateTitle = findViewById(R.id.calculateTitle); //货币名称
        calculateTitle.setText(itemTitle);

        calculateRes = findViewById(R.id.calculateRes); //结果
        calculateRMB = findViewById(R.id.calculateRMB); //输入RMB

        rate = Float.parseFloat(itemDetail);

        //计算汇率转化
        calculateRMB.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String str = editable.toString();
                if(str.length()>0){
                    //返回汇率换算后结果
                    calculateRes.setText(String.valueOf(Float.parseFloat(str)*(rate)));
                }else{
                    calculateRes.setText("");
                }
            }
        });

    }


}